package com.wmlive.hhvideo.heihei.personal.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import com.dyuiapi.api.DyUIAPI;
import com.dyuiapi.api.model.ShortVideoInfo;
import cn.wmlive.hhvideo.R;
import com.wmlive.hhvideo.common.base.DcBaseActivity;
import com.wmlive.hhvideo.heihei.personal.adapter.DraftBoxAdapter;
import com.wmlive.hhvideo.heihei.personal.util.SpaceItemDecoration;
import com.wmlive.hhvideo.widget.dialog.CustomDialog;
import com.wmlive.hhvideo.widget.refreshrecycler.OnRecyclerItemClickListener;
import com.wmlive.hhvideo.widget.refreshrecycler.RefreshRecyclerView;
import java.util.List;
import butterknife.BindView;

/**
 * Created by XueFei on 2017/6/3.
 * <p>
 * 草稿箱
 */

public class DraftBoxActivity extends DcBaseActivity implements DraftBoxAdapter.OnClickFansCustom, OnRecyclerItemClickListener {
    @BindView(R.id.rv_list)
    RefreshRecyclerView rvList;

    private List<ShortVideoInfo> list;
    private DraftBoxAdapter draftBoxAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_draftbox;
    }

    @Override
    protected void initData() {
        super.initData();
        setTitle(R.string.user_draftbox_title, true);

        // TODO: 2017/6/5 获取草稿箱数据
        list = DyUIAPI.getDraftShortVideoInfos(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        rvList.setLayoutManager(gridLayoutManager);
        draftBoxAdapter = new DraftBoxAdapter(list, rvList);
        rvList.addItemDecoration(new SpaceItemDecoration(8, false));
        rvList.setAdapter(draftBoxAdapter);
        rvList.setRefreshing(false);
        rvList.setLoadMoreEnable(false);

        draftBoxAdapter.setOnRecyclerItemClickListener(this);
        draftBoxAdapter.setOnClickCustom(this);
    }

    @Override
    protected void onSingleClick(View v) {

    }

    private CustomDialog customDialog;

    @Override
    public void onClick(View view, final int position) {
        if (view.getId() == R.id.iv_delete) {
            customDialog = new CustomDialog(this, R.style.BaseDialogTheme);
            customDialog.setTitle(R.string.dialog_draftbox_tip);
//            customDialog.setCancelable(false);
            customDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    customDialog.dismiss();

                    ShortVideoInfo shortVideoInfo = list.get(position);
                    shortVideoInfo.delete();
                    list.remove(position);

                    draftBoxAdapter.notifyDataSetChanged();
                }
            });
            customDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    customDialog.dismiss();
                }
            });
            customDialog.show();
        }
    }

    @Override
    public void onRecyclerItemClick(int dataPosition, View view, Object data) {
        DyUIAPI.continueShortVideoEdit(this, (ShortVideoInfo) data);
    }

    public static Intent getCallingIntent(Context context) {
        Intent intent = new Intent(context, DraftBoxActivity.class);
        return intent;
    }
}
